package com.sitanyun.merchant.guide.frament.view.activity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.base.ToastUtil;
import com.sitanyun.merchant.guide.bean.ForgetBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 添加银行卡
 */
public class BankInsertActivity extends BaseActivity {


    @BindView(R.id.bank_name)
    EditText bankName;
    @BindView(R.id.bank_number)
    EditText bankNumber;
    @BindView(R.id.bank_bank)
    EditText bankBank;
    @BindView(R.id.insert_bank)
    TextView insertBank;
    private String sfzid;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bank_insert;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("添加银行卡");
    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 添加
     */
    @OnClick(R.id.insert_bank)
    public void onViewClicked() {
        if (bankName.getText().toString().isEmpty()) {
            ToastUtil.showToast(BankInsertActivity.this, "请输入持卡人姓名");
            return;
        }
        if (bankNumber.getText().toString().isEmpty()) {
            ToastUtil.showToast(BankInsertActivity.this, "请输入本人银行卡号");
            return;
        }
     //三要素验证
        OkHttpUtils.get().url(Urls.transAuth)
                .addHeader("Authorization","Bearer "+ SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .addParams("realname",bankName.getText().toString())
                .addParams("certNo",SharedPreferenceUtil.getStringData("sfz"))
                .addParams("bankNo",bankNumber.getText().toString())

                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                ForgetBean baseEntity=   new Gson().fromJson(response, ForgetBean.class);
              if (baseEntity.getCode()==0){
                  HashMap<String,String> hashMap= new HashMap<String,String>();
                  hashMap.put("cardOwner",bankName.getText().toString().trim());
                  hashMap.put("bankCardNum",bankNumber.getText().toString().trim());
                  hashMap.put("bankName",bankBank.getText().toString().trim());
                  hashMap.put("individualId",SharedPreferenceUtil.getStringData("individ"));
                  //银行卡添加
                  OkHttpUtils.postString()
                          .mediaType(MediaType.parse("application/json; charset=utf-8"))
                          .addHeader("Authorization","Bearer "+ SharedPreferenceUtil.getStringData("token"))
                          .addHeader("Content-Type", "application/json")
                          .url(Urls.addcard)
                          .content(new Gson().toJson(hashMap))
                          .build().execute(new StringCallbacks() {
                      @Override
                      public void onError(Call call, Exception e, int id) {

                      }

                      @Override
                      public void onResponse(String response, int id) {
                          ForgetBean baseEntity=   new Gson().fromJson(response, ForgetBean.class);
                          if (baseEntity.getCode()==0){
                              ToastUtil.showToast(BankInsertActivity.this,"添加成功");

                              finish();
                          }else {
                              ToastUtil.showToast(BankInsertActivity.this,baseEntity.getMsg()+"");
                          }
                      }
                  });


              }else {
                  ToastUtil.showToast(BankInsertActivity.this,baseEntity.getMsg()+"");
              }
            }
        });



    }
}
